package com.example.jz.noone.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.jz.noone.utils.BaseActivity;
import com.example.jz.noone.utils.Code;
import com.example.jz.noone.R;
import com.example.jz.noone.utils.StatusBar;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends BaseActivity{

    @BindView(R.id.username)
    EditText username;
    @BindView(R.id.password1)
    EditText password1;
    @BindView(R.id.password2)
    EditText password2;
    @BindView(R.id.yanzheng)
    EditText yanzheng;
    @BindView(R.id.iv_showCode)
    ImageView ivShowCode;
    @BindView(R.id.register)
    Button register;
    @BindView(R.id.login)
    TextView login;
    private static final String API="http://192.168.30.120:8080/RsServlet";
    private String realCode;
    public static final String TAG = RegisterActivity.class.getName();
    private Toast mToast;
    private String Dte;
    private Context context;
    private Object init;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.register_main );
        ButterKnife.bind( this );
        ivShowCode.setImageBitmap( Code.getInstance().createBitmap() );
        realCode=Code.getInstance().getCode().toLowerCase();
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat( "yyyy年MM月dd日 HH:mm:ss" );// HH:mm:ss
        //获取当前时间
        Date date=new Date( System.currentTimeMillis() );
        Dte=simpleDateFormat.format( date );
        getinit();
    }


    private void getinit() {
        final String name = username.getText().toString();
        String pwd1 = password1.getText().toString();
        String pwd2 = password2.getText().toString();
        username.setOnFocusChangeListener( new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v,boolean hasFocus) {
                if (hasFocus){
                    showToast( "" );
                }else {

                }
            }
        } );


        password1.setOnFocusChangeListener( new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v,boolean hasFocus) {

            }
        } );

        password2.setOnFocusChangeListener( new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v,boolean hasFocus) {

            }
        } );

    }




    @OnClick({R.id.iv_showCode,R.id.register,R.id.login})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_showCode:
                ivShowCode.setImageBitmap(Code.getInstance().createBitmap());
                realCode = Code.getInstance().getCode().toLowerCase();
                Log.v(TAG,"realCode"+realCode);
                break;
            case R.id.register:
                String yz = yanzheng.getText().toString().toLowerCase();
                    if (yz.equals(realCode)){
                        getData();
                    }else {
                        showToast( "验证码错误!" );
                        ivShowCode.setImageBitmap(Code.getInstance().createBitmap());
                        realCode = Code.getInstance().getCode().toLowerCase();
                        Log.v(TAG,"realCode"+realCode);
                        return;
                    }
                break;
            case R.id.login:
                Intent intent = new Intent( RegisterActivity.this,LoginActivity.class);
                RegisterActivity.this.finish();
                startActivity( intent );
                break;
        }
    }








    private void getData() {
        String name = username.getText().toString();
        String pwd1 = password1.getText().toString();
        RequestParams params = new RequestParams(API);
        params.addParameter( "zuser",name);
        params.addParameter( "zpwd" ,pwd1);
        params.addParameter( "date",Dte);
        x.http().post( params,new Callback.CommonCallback <String>() {
            @Override
            public void onSuccess(String result) {
                System.out.println(result);
            }

            @Override
            public void onError(Throwable ex,boolean isOnCallback) {
                System.out.println(ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        } );
    }
    private void showToast(String msg) {
        if (mToast == null) {
            mToast=Toast.makeText( this,msg,Toast.LENGTH_SHORT );
        } else {
            mToast.setText( msg );
        }
        mToast.show();
    }
}
